#include "include/PowerLibraryEngine.h"
#include "Config.h"

#ifdef __COMPILE_WIN32__
    #include "Win32Device.h"
#endif

#ifdef __COMPILE_LINUX__
    #include "LinuxDevice.h"
#endif

ILog *LogClass								= NULL;
IGUI *GUIClass								= NULL;
IDriver *VideoDriverClass					= NULL;
ISceneManager *SceneManagerClass			= NULL;
ITimerManager *TimeManagerClass				= NULL;
IInputManager *InputManagerClass			= NULL;
IFileSystem *FileSystemClass				= NULL;
IDevice *DeviceClass						= NULL;
IStateManager *StateManagerClass			= NULL;
IConfigManager *ConfigManagerClass			= NULL;
IRandomGenerator *RandomGeneratorClass		= NULL;
IUsefulFunctions *UsefulFunctionsClass		= NULL;
ITextureManager *TextureManagerClass		= NULL;
IPackageManager *PackageManagerClass		= NULL;

POWERLIBRARYENGINE_API bool InitSystem()
{
//    StartDriverParams drvp;
//    CCommonDevice c(&drvp);

//    c.CreateHelpClasses();
	return true;
}

IDevice *InitPLEVideo(stringc FileName,StartDriverParams *stp)
{
	stp->UseVertexArray = false;

    DeviceClass = NULL;
#ifdef __COMPILE_WIN32__
	DeviceClass = new CWin32Device(stp);
#endif

#ifdef __COMPILE_LINUX__
    DeviceClass = new CLinuxDevice(stp);
#endif

	PLE_FileSystem->ReadActualDirectory();

    ConfigManager->SetActualSection("GlobalVariables");

    if (ConfigManager->ReadFile(FileName))
    {
        stp->LenX = ConfigManager->GetVarAsInt("Video","SizeX");
        stp->LenY = ConfigManager->GetVarAsInt("Video","SizeY");
        stp->FullScreen = ((ConfigManager->GetVarAsInt("Video","FullScreen")!=0)?true:false);
        stp->TitleDefault = (s8*)(ConfigManager->GetVarAsString("Video","WindowTitle").c_str());
        stp->drvType = (DRV_ENUM)ConfigManager->GetVarAsInt("Video","PreferredDriver");
        stp->VSync = ((ConfigManager->GetVarAsInt("Video","VSync")!=0)?true:false);

        u32 Logs = LOG_NONE;
        u32 log = ConfigManager->GetVarAsInt("Log","UseLogDebug");
        if (log)
			Logs |= LOG_DEBUG;

		log = ConfigManager->GetVarAsInt("Log","UseLogInfo");
        if (log)
			Logs |= LOG_INFO;

		log = ConfigManager->GetVarAsInt("Log","UseLogRelease");
        if (log)
			Logs |= LOG_RELEASE;


		log = ConfigManager->GetVarAsInt("Log","UseLogDebugError");
        if (log)
			Logs |= LOG_DEBUG_ERROR;

		log = ConfigManager->GetVarAsInt("Log","UseLogReleaseError");
        if (log)
			Logs |= LOG_RELEASE_ERROR;

		stp->LogsType = (LOGS_TYPE)Logs;
		Log->SetActivesLogs(stp->LogsType);
    }

    DeviceClass->CreateCommonWindow();

	return DeviceClass;
}

void JoinSystem()
{
}

bool UpdateSystem()
{
}

IDevice *GetDevice()
{
	return DeviceClass;
}

void SetDevice(IDevice *nv)
{
	DeviceClass = nv;
}

void DestroyDevice()
{
	if (DeviceClass)
	{
		delete DeviceClass;
		DeviceClass = NULL;
	}
#ifdef _MSC_VER
//	_CrtDumpMemoryLeaks();
#endif
}

IInputManager *GetInputManager()
{
	return InputManagerClass;
}

void SetInputManager(IInputManager *nv)
{
	InputManagerClass = nv;
}

void DeleteInputManager()
{
	DEALLOCATE(InputManagerClass);
}

ISceneManager *GetSceneManager()
{
	return SceneManagerClass;
}

void SetSceneManager(ISceneManager *nv)
{
	SceneManagerClass = nv;
}

void DeleteSceneManager()
{
	DEALLOCATE(SceneManagerClass);
}

IFileSystem *GetFileSystem()
{
	return FileSystemClass;
}

void SetFileSystem(IFileSystem *nv)
{
	FileSystemClass = nv;
}

void DeleteFileSystem()
{
	DEALLOCATE(FileSystemClass);
}

IDriver *GetVideoDriver()
{
	return VideoDriverClass;
}

void SetVideoDriver(IDriver *nv)
{
	VideoDriverClass = nv;
}

void DeleteVideoDriver()
{
	DEALLOCATE(VideoDriverClass);
}

ITimerManager *GetTimeManager()
{
	return TimeManagerClass;
}

void SetTimeManager(ITimerManager *nv)
{
	TimeManagerClass = nv;
}

void DeleteTimeManager()
{
	DEALLOCATE(TimeManagerClass);
}

ILog *GetLog()
{
	return LogClass;
}

void SetLog(ILog *nv)
{
	LogClass = nv;
}

void DeleteLog()
{
	DEALLOCATE(LogClass);
}

IGUI *GetGUI()
{
	return GUIClass;
}

void SetGUI(IGUI *ng)
{
	GUIClass = ng;
}

void DeleteGUI()
{
	DEALLOCATE(GUIClass);
}

IStateManager *GetStateManager()
{
    return StateManagerClass;
}

void SetStateManager(IStateManager *nsm)
{
    StateManagerClass = nsm;
}

void DeleteStateManager()
{
    DEALLOCATE(StateManagerClass);
}

IConfigManager *GetConfigManager()
{
    return ConfigManagerClass;
}

void SetConfigManager(IConfigManager *ncm)
{
    ConfigManagerClass = ncm;
}

void DeleteConfigManager()
{
    DEALLOCATE(ConfigManagerClass);
}

IRandomGenerator *GetRandomGenerator()
{
    return RandomGeneratorClass;
}

void SetRandomGenerator(IRandomGenerator *nrg)
{
    RandomGeneratorClass = nrg;
}

void DeleteRandomGenerator()
{
    DEALLOCATE(RandomGeneratorClass);
}

IUsefulFunctions *GetUsefulFunctions()
{
	return UsefulFunctionsClass;
}

void DeleteUsefulFunctions()
{
	DEALLOCATE(UsefulFunctionsClass);
}

void SetUsefulFunctions(IUsefulFunctions *nuf)
{
	UsefulFunctionsClass = nuf;
}

ITextureManager *GetTextureManager()
{
	return TextureManagerClass;
}

void DeleteTextureManager()
{
	DEALLOCATE(TextureManagerClass);
}

void SetTextureManager(ITextureManager *ntm)
{
	TextureManagerClass = ntm;
}

IPackageManager *GetPackageManager()
{
	return PackageManagerClass;
}

void DeletePackageManager()
{
	DEALLOCATE(PackageManagerClass);
}

void SetPackageManager(IPackageManager *npm)
{
	PackageManagerClass = npm;
}
